# import mitmproxy.http
# from mitmproxy import ctx
# _*_ coding:utf-8 _*_
import os
import io
from urllib.parse import quote,unquote

# mitmweb -s HTTPProxy.py 
def response(flow):
    request_url = unquote(flow.request.url, "utf-8")
    if '/wbdflightlist' in request_url:
        # print(flow.response.text)
        request_url_map = request_url.split('?')
        param_map = request_url_map[1].split('&')
        departureCity = param_map[0].split('=')[1]
        arrivalCity = param_map[1].split('=')[1]
        fo = open("data/wbdflightlist/" +  departureCity + "-" + arrivalCity + ".json", "w")
        fo.write(flow.response.text)
        # 关闭文件
        fo.close()
    
    # b("获取直飞数据成功");
    if 'domestic_oneway_new' in request_url:
        # b("获取直飞数据成功");
        f = open("xss.js")
        content = f.read()
        # print(content)
        flow.response.text = flow.response.text.replace('b("获取直飞数据成功")', 'b("获取直飞数据成功");' + content) 
        